package cn.tqfeiyang.springplus.system.controller;

import cn.tqfeiyang.springplus.system.service.RoleService;
import cn.tqfeiyang.springplus.system.request.RoleDTO;
import cn.tqfeiyang.springplus.system.response.RoleVO;
import cn.tqfeiyang.springplus.system.request.RoleSTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.springplus.framework.response.ApiResult;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import cn.tqfeiyang.springplus.framework.validation.WhenCreate;
import cn.tqfeiyang.springplus.framework.validation.WhenUpdate;
import cn.tqfeiyang.springplus.framework.validation.EntityId;
import cn.tqfeiyang.springplus.framework.security.JwtSecurityRequirement;
import cn.tqfeiyang.springplus.framework.logging.LoggingRequirement;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 角色前端控制器
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/api/system/role")
@Tag(name = "系统/角色")
public class RoleController {

    /**
     * 角色服务
     */
    @Resource
    private RoleService roleService;

    /**
     * 查询角色
     *
     * @param roleSTO 查询数据
     * @return 角色列表
     */
    @GetMapping("/query")
    @Operation(summary = "查询角色")
    @PreAuthorize("hasAnyAuthority('system:role:query')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<PageData<RoleVO>> selectRolePage(RoleSTO roleSTO) {
        return ApiResult.success(roleService.selectRolePage(roleSTO));
    }

    /**
     * 读取角色
     *
     * @param roleId 角色id
     * @return 角色信息
     */
    @GetMapping("/read")
    @Operation(summary = "读取角色")
    @PreAuthorize("hasAnyAuthority('system:role:read')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<RoleVO> selectRoleInfo(@RequestParam @EntityId Long roleId) {
        return ApiResult.success(roleService.selectRoleInfo(roleId));
    }

    /**
     * 新建角色
     *
     * @param roleDTO 请求数据
     * @return 角色信息
     */
    @PostMapping("/create")
    @Operation(summary = "新建角色")
    @PreAuthorize("hasAnyAuthority('system:role:create')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<RoleVO> createRole(@RequestBody @Validated(WhenCreate.class) RoleDTO roleDTO) {
        Long roleId = roleService.createRole(roleDTO);
        return ApiResult.success(roleService.selectRoleInfo(roleId));
    }

    /**
     * 修改角色
     *
     * @param roleId 角色id
     * @param roleDTO 请求数据
     * @return 角色信息
     */
    @PostMapping("/update")
    @Operation(summary = "修改角色")
    @PreAuthorize("hasAnyAuthority('system:role:update')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<RoleVO> updateRole(@RequestParam @EntityId Long roleId, @RequestBody @Validated(WhenUpdate.class) RoleDTO roleDTO) {
        roleService.updateRole(roleId, roleDTO);
        return ApiResult.success(roleService.selectRoleInfo(roleId));
    }

    /**
     * 删除角色
     *
     * @param roleId 角色id
     */
    @PostMapping("/delete")
    @Operation(summary = "删除角色")
    @PreAuthorize("hasAnyAuthority('system:role:delete')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult deleteRole(@RequestParam @EntityId Long roleId) {
        roleService.deleteRole(roleId);
        return ApiResult.success();
    }
}
